package com.fqgj.sentry.controller.mc;

import com.fqgj.sentry.biz.mc.RoleBusiness;
import com.fqgj.sentry.common.cache.RequestLocalInfo;
import com.fqgj.sentry.common.response.ApiOutput;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.controller.mc.request.AddRoleVo;
import com.fqgj.sentry.controller.mc.response.PermissionMenuResponse;
import com.fqgj.sentry.manage.domain.PermissionMenuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by liupengpeng on 2017/7/14.
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleBusiness roleBusiness;


    /**
     * 角色集合
     *
     * @param apiPage
     * @return
     */
    @PreAuthorize("hasPermission('','mc_role_read')")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiOutput list(@RequestBody ApiPage apiPage) {
        ApiPage apiPage1 = roleBusiness.queryRoleListForPage(RequestLocalInfo.getCurrentMerchantId(), apiPage);
        return new ApiOutput<>(apiPage1);
    }

    /**
     * 角色集合
     *
     * @return
     */
    @PreAuthorize("hasPermission('','mc_role_read')")
    @RequestMapping(value = "/{roleId}/permission/list", method = RequestMethod.POST)
    public ApiOutput permissionList(@PathVariable(value = "roleId") Long roleId) {
        List<PermissionMenuInfo> permissionMenuInfoList = roleBusiness.findListByRoleId(roleId);
        return new ApiOutput<>(new PermissionMenuResponse(permissionMenuInfoList));
    }


    /**
     * 新增角色
     *
     * @param addRoleVo
     * @return
     */
    @PreAuthorize("hasPermission('','mc_role_write')")
    @RequestMapping(value = "/permission/insert", method = RequestMethod.POST)
    public ApiOutput save(@RequestBody AddRoleVo addRoleVo) {
        roleBusiness.addRolePermission(addRoleVo);
        return new ApiOutput<>();
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @PreAuthorize("hasPermission('','mc_role_write')")
    @RequestMapping(value = "/{roleId}/permission/delete", method = RequestMethod.POST)
    public ApiOutput delete(@PathVariable(value = "roleId") Long roleId) {
        roleBusiness.delete(roleId);
        return new ApiOutput<>();
    }

    /**
     * 修改角色
     *
     * @return
     */
    @PreAuthorize("hasPermission('','mc_role_write')")
    @RequestMapping(value = "/permission/update", method = RequestMethod.POST)
    public ApiOutput update(@RequestBody AddRoleVo addRoleVo) {
        roleBusiness.updateRolePermission(addRoleVo);
        return new ApiOutput<>();
    }

}
